package com.qianniu.napi.admin.web;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.qianniu.napi.admin.entity.Jinfo;
import com.qianniu.napi.admin.entity.Order;
import com.qianniu.napi.admin.entity.OrderRefund;
import com.qianniu.napi.admin.service.*;
import com.qianniu.napi.common.util.ObjectUtil;
import com.qianniu.napi.common.util.StringUtil;
import nl.bitwalker.useragentutils.UserAgent;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;
import java.util.Date;

/**
 * <p>
 * 订单退款
 * </p>
 *
 * @author chenxuebin
 * @since 2018-08-22
 */
@RestController
public class OrderRefundController {

    private static final Logger logger = LoggerFactory.getLogger(OrderRefundController.class);

    @Autowired
    private IOrderRefundService orderrefundService;
    @Autowired
    private IOrderService orderService;
    @Autowired
    private IMerchantAppService merchantAppService;
    @Autowired
    private IQnpay qnpay;
    @Autowired
    private IRedisService redisService;

    @RequestMapping("/api/orderrefund/list")
    @ResponseBody
    public Jinfo list(@RequestParam(value = "status", required = false) String status,
                      @RequestParam(value = "pageIndex", required = false) Integer pageIndex,
                      @RequestParam(value = "pageSize", required = false) Integer pageSize,
                      HttpServletRequest req) {
        Jinfo info = new Jinfo();
        if (pageSize == null) pageSize = 10;
        Page page = new Page(pageIndex, pageSize);
        page = orderrefundService.selectPage(page, new EntityWrapper<OrderRefund>()
                .eq("uid", ObjectUtil.getUid())
                .eq("state", 1));
        info.setInfo(page.getRecords());
        return info;
    }

    @RequestMapping("/api/orderrefund/add")
    @ResponseBody
    public Jinfo orderrefundAdd(@Valid OrderRefund orderrefund, BindingResult bindingResult, HttpServletRequest req) {
        Jinfo info = new Jinfo();
        // 字段校验
        if (bindingResult.hasErrors()) {
            logger.debug("There are errors! {}", bindingResult);
            info.setMsg("There are errors! " + bindingResult);
            info.setCode(400);
            return info;
        }
        orderrefund.setRefundNo(StringUtil.getSnNo("T"));
        orderrefund.setCreatetime(new Date());
        orderrefund.setStatus(1);
        orderrefund.setState(1);
        orderrefund.setUid(ObjectUtil.getUid());
        orderrefund.setMid(ObjectUtil.getMid());
        orderrefund.setMpid(ObjectUtil.getMpid());
        String ip = ObjectUtil.getIP();
        orderrefund.setIp(ip);// 赋值：  js: feedback.ip = ip

        UserAgent userAgent = UserAgent.parseUserAgentString(req.getHeader("User-Agent"));
        orderrefund.setTerminal(userAgent.getOperatingSystem().toString());

        orderrefund.insert();

        Order order = new Order();
        order.setRefundNo(orderrefund.getRefundNo());
        order.setId(orderrefund.getOid());
        order.updateById();

        return info;
    }

    @RequestMapping("/api/orderrefund/modify")
    @ResponseBody
    public Jinfo orderrefundUpdate(HttpServletRequest req,
                                   OrderRefund orderrefund) {
        Jinfo info = new Jinfo();
        orderrefund.setModifytime(new Date());
        orderrefund.updateById();
        return info;
    }

    @RequestMapping("/api/orderrefund/detail")
    @ResponseBody
    public Jinfo getOrderRefundDetailById(@RequestParam("token") String token,
                                          @RequestParam("id") long id, HttpServletRequest req
    ) {
        Jinfo info = new Jinfo();
        OrderRefund orderrefund = orderrefundService.selectById(id);
        info.setInfo(orderrefund);
        return info;
    }

}
